﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web;
using Shuba.Business;
using Shuba.Model;
using System.Collections.Generic;

namespace Shuba.Web.mobile
{
    public partial class branch_device_add : basePage
    {
        string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtIMEI.ClientID;
            this.Form.DefaultButton = btnSubmit.UniqueID;
            
            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("MSTR_BRANCH_DEVICE");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("MSTR_BRANCH_DEVICE");

                BindBranchData();

                if (Request.QueryString["a"] == "ed")
                {
                    txtIMEI.Enabled = false;
                    BindData();
                }                
            }
        }

        private void BindData()
        {            
            string imei = Request.QueryString["i"];

            BranchDevice b = new BranchDevice();
            BranchDeviceInfo info = b.Get(imei);

            hdIMEI.Value = imei;
            txtIMEI.Text = imei;
            ddlBranchID.SelectedValue = info.BranchID;            
        }

        private void BindBranchData()
        {
            Branch bl = new Branch();
            List<BranchInfo> list = bl.GetList("");
            ddlBranchID.DataSource = list;
            ddlBranchID.DataTextField = "BranchName";
            ddlBranchID.DataValueField = "BranchID";
            ddlBranchID.DataBind();
            ddlBranchID.Items.Insert(0, "- Pilih dengan tepat dan benar -");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (ddlBranchID.SelectedIndex < 1)
                {
                    throw new Exception("Branch is required");
                }

                BranchDevice d = new BranchDevice();                                                
                string imei = txtIMEI.Text;
                string branchID = ddlBranchID.SelectedValue;
                string actType = "INSERT";

                BranchDeviceInfo info = null;

                /*
                if (d.GetByIMEI(imei) != null)                
                    throw new Exception("IMEI already exists!");*/

                if (Request.QueryString["a"] == "ed")
                {                    
                    info = d.Get(hdIMEI.Value);
                    if (info == null)
                        throw new Exception("Branch Device not found!");
                    
                    actType = "UPDATE";
                }
                else
                {
                    info = d.Get(imei);
                    if (info != null)
                        throw new Exception("Imei already used!");

                    info = new BranchDeviceInfo();                    
                    info.IMEI = imei;                    
                }

                info.BranchID = branchID;                               
                info.EntryBy = HttpContext.Current.User.Identity.Name;
                info.EntryDate = ProcessingDate();
                info.UpdateBy = HttpContext.Current.User.Identity.Name;
                info.UpdateDate = ProcessingDate();
                info.ActType = actType;                                
                d.Save(info);

                AddLog(actType, actType + " Branch Device");
                msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                AlertAndRedirect(msg, "branch_device_list.aspx");
            }
            catch (Exception ex)
            {
                ErrBox.Visible = true;
                ErrMsg.Text = ex.Message;
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("branch_device_list.aspx");
        }
    }
}
